package cn.fhkj.util;

import cn.fhkj.bean.DocConstant;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;
import sun.misc.BASE64Encoder;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class DocUtil {
	public Configuration configure = null;

	public DocUtil() {
		configure = new Configuration(Configuration.VERSION_2_3_26);
		configure.setDefaultEncoding("utf-8");
	}

	/**
	 * 根据Doc模板生成word文件
	 * 
	 * @param dataMap
	 *            需要填入模板的数据
	 * @param downloadType
	 *            文件名称
	 * @param savePath
	 *            保存路径
	 */
	public void createDoc(Map<String, Object> dataMap, String downloadType, String savePath) {
		try {
			// 加载需要装填的模板
			Template template = null;
			// 设置模板装置方法和路径，FreeMarker支持多种模板装载方法。可以从servlet，classpath,数据库装载。
			// 加载模板文件，放在resources下
			configure.setClassForTemplateLoading(this.getClass(), "/");
			// 设置对象包装器
			// configure.setObjectWrapper(new DefaultObjectWrapper());
			// 设置异常处理器
			configure.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);
			// 定义Template对象，注意模板类型名字与downloadType要一致
			template = configure.getTemplate(downloadType + ".xml");
			File outFile = new File(savePath);
			Writer out = null;
			// 指定编码表需使用转换流，转换流对象要接收一个字节输出流
			out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"));
			template.process(dataMap, out);
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (TemplateException e) {
			e.printStackTrace();
		}
	}

	public String getImageStr(String imgFile) {
		InputStream in = null;
		byte[] data = null;
		try {
			in = new FileInputStream(imgFile);
			data = new byte[in.available()];
			in.read(data);
			in.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		BASE64Encoder encoder = new BASE64Encoder();
		return encoder.encode(data);
	}

	public static void main(String[] args) {
		DocUtil docUtil = new DocUtil();
		Map<String, Object> dataMap = new HashMap<String, Object>();
		List<Object> list=new ArrayList<>();
		list.add( "对方水电费第三方我说的吃饭1 乙方在甲方任职期间，必须遵守公司规定的任何成文及不成文的保密规章、制度，履行与其工作岗位相应的保密职责。");
		list.add( "对方水电费第三方我说的吃饭2 乙方在甲方任职期间，必须遵守公司规定的任何成文及不成文的保密规章、制度，履行与其工作岗位相应的保密职责。");
//		
//		dataMap.put("orgFullName", "北京法和科技");
//		dataMap.put("regAddress", "北京朝阳区创业大厦");
//		dataMap.put("legalPerson", "与君一战,三生有幸");
//		dataMap.put("eName", "风清扬");
//		dataMap.put("employeeID", "111111111222");
//		dataMap.put("liveAddress", "与君一战,三生有幸");
//		dataMap.put("situationList", list);
//		dataMap.put("year", "无限");
//		dataMap.put("informationMap",DocConstant.CONFIDENTIAL_INFORMATION);
		
		dataMap.put("orgFullName", "北京法和科技");
		dataMap.put("regAddress", "北京朝阳区创业大厦");
		dataMap.put("legalPerson", "与君一战,三生有幸");
		dataMap.put("eName", "风清扬");
		dataMap.put("eID", "111111111222");
		dataMap.put("liveAddress", "与君一战,三生有幸");
		dataMap.put("domicilePlace", "与君一战,三生有幸");
		dataMap.put("contactInformation", "18811223344");
		dataMap.put("year", "2017");
		dataMap.put("month", "11");
		dataMap.put("day", "13");
		dataMap.put("duration", "10");
		dataMap.put("probation",String.format(DocConstant.PROBATION_STATEMENT.get(DocConstant.PROBATION_STATEMENT_SECOND), "3") );
		dataMap.put("jobTitle", "web开发");
		dataMap.put("positionStatement", String.format(DocConstant.POSITION_STATEMENT_MAP.get(DocConstant.POSITION_STATEMENT_SECOND), "233"));
		dataMap.put("workPlace", "沧海");
		dataMap.put("salary", String.format(DocConstant.SALARY_MAP.get(DocConstant.SALARY_FIRST), "100000"));
		dataMap.put("salaryPro",DocConstant.SALARY_PRO_MAP.get(DocConstant.SALARY_PRO_SECOND) +"<w:br/>"
								+String.format(DocConstant.SALARY_PRO_MAP.get(DocConstant.SALARY_PRO_THIRD), "基本","50000")+"<w:br/>"
								+String.format(DocConstant.SALARY_PRO_MAP.get(DocConstant.SALARY_PRO_THIRD), "绩效","50000")
					);
		dataMap.put("theDay", "5");
		dataMap.put("time", "上月");
		dataMap.put("rewardMethod", "a+b=c");
		dataMap.put("welfare", "餐补");
		dataMap.put("terminateContractMap", DocConstant.TERMINATE_CONTRACT_MAP);
		dataMap.put("situationList",list);
		dataMap.put("agreementMap", DocConstant.AGREEMENT_MAP);
		dataMap.put("conditionList", list);
		dataMap.put("appoint", "撒的发生都感到是的发送到发送到");
		
		long suffix = System.currentTimeMillis();

//		docUtil.createDoc(dataMap, "test", "D:\\保密协议.doc");
		
		docUtil.createDoc(dataMap, "test2", "D:\\劳动合同"+suffix+".doc");
		File file=new File("D:\\劳动合同"+suffix+".doc");
		long length = file.length();
		String fileSize=Math.round(length*0.1/1024*10)+"kb";
		System.err.println(fileSize);
		String fileName="劳动合同"+suffix+".doc";
		System.err.println(fileName);
	}
	
	/* public static boolean writeWordFile() {
		 
		  boolean w = false;
		  String path = "c:/";
		  
		  try {
		   if (!"".equals(path)) {
		    
		    // 检查目录是否存在
		    File fileDir = new File(path);
		    if (fileDir.exists()) {
		     
		     // 生成临时文件名称
		     String fileName = "a.doc";
		     String content = "<html>" +
		           "<head>你好</head>" +
		          "<body>" +
		            "<table>" +
		             "<tr>" +
		              "<td>信息1</td>" +              
		              "<td>信息2</td>" +              
		              "<td>t3</td>" +              
		             "<tr>" +
		            "</table>" +
		            "</body>" +
		            "</html>";
		     
		     byte b[] = content.getBytes();
		     ByteArrayInputStream bais = new ByteArrayInputStream(b);
		     POIFSFileSystem poifs = new POIFSFileSystem();
		     DirectoryEntry directory = poifs.getRoot();
		     DocumentEntry documentEntry = directory.createDocument("WordDocument", bais);
		     FileOutputStream ostream = new FileOutputStream(path+ fileName);
		     poifs.writeFilesystem(ostream);
		     bais.close();
		     ostream.close();
		     
		    }
		   }
		 
		  } catch (IOException e) {
		   e.printStackTrace();
		  }
		 
		  return w;
	 }*/
}
